package com.lht.arithmetic.recursion;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 测试递归的时候 每层公用一个对象的时候，是否会冲突
 *
 * @author Eric
 * @version 1.0
 * @date 2019-11-23
 */
public class Test {


    public static void main(String[] args) {

        Random ran = new Random();
        List<Tree> third = new ArrayList<>();
        List<Tree> third1 = new ArrayList<>();

        for (int i = 0; i < 1; i++) {
            third.add(new Tree(100));
            third1.add(new Tree(100));
        }

        List<Tree> s = new ArrayList<>();
        Tree s1 = new Tree(100, third);
        Tree s2 = new Tree(100, third1);
        s.add(s1);
        s.add(s2);

        Tree f = new Tree(ran.nextInt(1000), s);

        System.out.println(recursion(f, new int[1])[0]);
    }

    public static int[] recursion(Tree f, int[] arr) {
        int[] a = new int[1];
        if (f.getChilds() != null && !f.getChilds().isEmpty()) {
            for (Tree t : f.getChilds()) {
                arr[0] += recursion(t, a)[0];
            }
        }
        a[0] += f.getValue();
        return a;
    }


    public static class Tree {

        private int value;

        List<Tree> childs;

        public Tree(int value) {
            this(value, null);
        }

        public Tree(int value, List<Tree> childs) {
            this.value = value;
            this.childs = childs;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public List<Tree> getChilds() {
            return childs;
        }

        public void setChilds(List<Tree> childs) {
            this.childs = childs;
        }
    }

}
